package cn.cool.domain.activity.service.trial;

import cn.cool.types.design.framework.tree.AbstractStrategyRouter;
import cn.cool.types.design.framework.tree.StrategyHandler;
import cn.cool.types.design.framework.tree.StrategyMapper;
import cn.hutool.core.util.ObjUtil;

public abstract class AbstractMultiThreadStrategyRouter<T, D, R>
    implements StrategyMapper<T, D, R>, StrategyHandler<T, D, R> {

    protected StrategyHandler<T, D, R> defaultStrategyHandler = StrategyHandler.DEFAULT;

    public R router(T requestParameter, D dynamicContext) throws Exception {
        StrategyHandler<T, D, R> strategyHandler = get(requestParameter, dynamicContext);
        if (ObjUtil.isNotNull(strategyHandler)) {
            return strategyHandler.apply(requestParameter, dynamicContext);
        }
        return defaultStrategyHandler.apply(requestParameter, dynamicContext);
    }

    @Override
    public R apply(T requestParameter, D dynamicContext) throws Exception {
        // 异步加载数据
        multiThread(requestParameter, dynamicContext);
        // 业务逻辑受理
        return doApply(requestParameter, dynamicContext);
    }

    /**
     * 异步加载数据
     */
    protected abstract void multiThread(T requestParameter, D dynamicContext) throws Exception;

    /**
     * 业务流程受理
     */
    protected abstract R doApply(T requestParameter, D dynamicContext) throws Exception;
}
